$(function(){

	init();
	//初始化数据
	function init(){

		$.get(info_url, function(data){
	      
	      }, "json").error(function(data) {
	        var status_code = $.parseJSON(data.responseText);
	        layer.msg(status_code.message);
	      }).success(function(data) {
	            var obj = data.data;
				if(obj.city){
					city_Main(city_param,null);
				}else{
					return false;
				}
				if(obj.district){
					district_Main(district_param,null);
				}else{
					return false;
				}
	      })
	    
		province_Main(province_param,null);
		var city = $("#city").val();
		var district = $("#district").val();
	    if(city){
	    	city_Main(city_param,null);
	    }else{
					return false;
				}
	    if(district){
	    	district_Main(district_param,null);
	    }else{
			return false;
		}

	}

	//获取城市的数据
	$("#province").change(function(){
		$("#city,#district").empty();
		var province_val =  $(this).val();
		city_Main(null,province_val);
	})

	//获取地区的数据
	$("#city").change(function(){
		$("#district").empty();
		var city_val =  $(this).val();
		district_Main(null,city_val);
	})
	//获取要保存的数据
	$("#seveLawyer").click(function(){
		var params = getParams("#user_info_form");
		var loading = layer.msg('加载中...', {icon: 16});
		$.post(jumpUrl+"/center/customer/info?captcha="+Math.random(),params,function(data){
	      },'json').error(function(data) {
	        var status_code = $.parseJSON(data.responseText);
	        layer.msg(status_code.message)
	      }).success(function(data) {
	      		layer.close(loading);
	      		layer.msg("提交成功");	
	      		window.location.reload();     
	      });
				
	})

	var format_email = /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2} |net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT|cn|CN)$/;
	var format_mobile = /1[0-9]{10}$/;

	function clickValPhoe(params){
		if(params.phone == null || params.phone == ""){
			$(".errortip").text("用户名不能为空！");
			 return false;
		}else{
			if(params.phone.match(format_mobile)==null){
				$(".errortip").text("请输入正确的手机号码!");
				return false;
			}
		}
		if(params.phonecode == null || params.phonecode == ""){
			$(".errortip").text("请输入验证码");
			 return false;
		}
		$(".errortip").text("");
		return true;
	}

	function clickValEmail(params){
		if(params.email == null || params.email == ""){
			$(".errortip").text("邮箱账号不能为空！");
			 return false;
		}else{
			if(params.email.match(format_email)==null){
				$(".errortip").text("请输入正确邮箱账号!");
				return false;
			}
		}
		$(".errortip").text("");
		return true;
	}

	/*编辑姓名*/
	$(".amend-name").click(function(){
		var layer1 = layer.open({
			title:'编辑姓名',
		    type: 1,
		    skin: 'layui-layer-rim', //加上边框
		    shadeClose: false,  //是否点击遮罩关闭
		    area: ['atuo', 'auto'], //宽高
		    content: $('#nikname-tmpl').html()
		});
	})

	/*添加手机号*/
	$("#addPhone").click(function(){
		 var index1 = layer.open({
			title:'添加手机号',
		    type: 1,
		    skin: 'layui-layer-rim', //加上边框
		    shadeClose: false,  //是否点击遮罩关闭
		    area: ['atuo', 'auto'], //宽高
		    content: $('#phone-tmpl').html()
		});

	})

	/*修改手机号*/
	$("#updatePhone").click(function(){
		var layer1 = layer.open({
			title:'修改手机号',
		    type: 1,
		    skin: 'layui-layer-rim', //加上边框
		    shadeClose: false,  //是否点击遮罩关闭
		    area: ['atuo', 'auto'], //宽高
		    content: $('#phone-tmpl').html()
		});
	})

	/*立即绑定*/
	$("#nowBindEmail").click(function(){
		var layer1 = layer.open({
			title:'立即绑定',
		    type: 1,
		    skin: 'layui-layer-rim', //加上边框
		    shadeClose: false,  //是否点击遮罩关闭
		    area: ['atuo', 'auto'], //宽高
		    content: $('#email-tmpl').html()
		});
	})

	/*修改邮箱号*/
	$("#updateEmail").click(function(){
		var layer4 = layer.open({
			title:'修改邮箱号',
		    type: 1,
		    skin: 'layui-layer-rim', //加上边框
		    shadeClose: false,  //是否点击遮罩关闭
		    area: ['atuo', 'auto'], //宽高
		    content: $('#email-tmpl').html()
		});
	})

	$(document).on( "click", "#sevePhone", function(e) { 
		var params = getParams("#phone_form"); 
		var addPhone_url = jumpUrl+"/center/update_phone";

		if(clickValPhoe(params)){
			$.post(addPhone_url,params, function(data){
          
		      }, "json").error(function(data) {
		        var status_code = $.parseJSON(data.responseText);
		        layer.msg(status_code.message);
		      }).success(function(data) {
		          var obj = data.data;
		          layer.closeAll();
		          layer.msg(obj);
		          layer.msg("修改成功!");
				  window.location.reload();
		      }).error(function(data){
		      	var obj = $.parseJSON(data.responseText);
		      	if(obj.error_messages.phone != undefined){
					$(".errortip").text(obj.error_messages.phone)
		      	}else{
		      		$(".errortip").text(obj.error_messages.phonecode)
		      	}
		      	
		      })

			$('#phone-tmpl').hide();
			clearForm('#phone-tmpl');
			
		}else{
			return false;
		}
		stopDefault(e);
	})

	
	//获取手机验证码
	$(document).on( "click", "#chekePhoneCode", function(e) {
		$('#errortip').text("");
		var params = getParams("#phone_form"); 
		var phonecode_href = $(this).attr("data-href");
		$.post(phonecode_href, {"phone":params.phone, "_token":params.token}, function(data){

		}).error(function(data) {
		    var obj = $.parseJSON(data.responseText);
		    $('#errortip').show();
			$('#errortip').text(item[0]);
		}).success(function(data) {
			 alert(data.success_messages);
		});
		stopDefault(e);
	})

	$(document).on( "click", "#seveEmail", function(e) { 
		var params = getParams("#email_form"); 
		var addEmail_url = jumpUrl+"/center/update_email";
		if(clickValEmail(params)){
			$.post(addEmail_url,params, function(data){
          
		      }, "json").error(function(data) {
		        var status_code = $.parseJSON(data.responseText);
		        layer.msg(status_code.message);
		      }).success(function(data) {
		          var obj = data.data;
		          layer.closeAll();
		          layer.msg(obj);
		          $('#email-tmpl').hide();
				clearForm('#email-tmpl');
				alert("邮件已发送，请进入邮箱验证！");
				window.location.reload();
		      })
			
		}else{
			return false;
		}
		stopDefault(e);
	})
	//提交修改的名字
	$(document).on( "click", "#seveRealname", function(e) { 
		var params = getParams("#nikname_form"); 
		var addRealname_url = jumpUrl+"/center/customer/info?captcha="+Math.random();
			$.post(addRealname_url,params, function(data){
		      }, "json").error(function(data) {
		        var status_code = $.parseJSON(data.responseText);
		        layer.msg(status_code.message);
		      }).success(function(data) {
		        var obj = data.data;
		        $('#nikname-tmpl').hide();
				clearForm('#nikname-tmpl');
				alert("昵称修改成功");
				window.location.reload();
		        layer.closeAll();

		      })
			
		stopDefault(e);
	})

	//获取省份的数据
	function province_Main(province,_this){
		var province_url = jumpUrl+"/area/province";
		$.get(province_url, function(data){
          
	      }, "json").error(function(data) {
	        var status_code = $.parseJSON(data.responseText);
	        layer.msg(status_code.message);
	      }).success(function(data) {
	          var obj = data.data;
	          var options = '';
              $.each(obj, function(key, item) {
              		options+='<option value="'+item.key+'">'+item.value+'</option>';
              });
              $("#province").append(options);
              if(province){
              	 $("#province option[value='"+province+"']").attr("selected",true);
              }
             
	      });
	}
    //获取城市的数据
    function city_Main(city,_this){
    	var city_url;
    	if(!_this){
    		city_url = jumpUrl+"/area/"+province_param+"/city";
    	}else{
    		city_url = jumpUrl+"/area/"+_this+"/city";
    	}
		 
		$.get(city_url, function(data){
		  
		  }, "json").error(function(data) {
		    var status_code = $.parseJSON(data.responseText);
		    layer.msg(status_code.message);
		  }).success(function(data) {
		      var obj = data.data;
		      var options = '';
		      $.each(obj, function(key, item) {
		      		options+='<option value="'+item.key+'">'+item.value+'</option>';
		      });
		      $("#city").append(options);
		      if(city){
		      	$("#city option[value='"+city+"']").attr("selected",true);
		      }
		  });
	} 

	//获取地区的数据
	function district_Main(district,_this){
		var county_url;
    	if(!_this){
    		county_url = jumpUrl+"/area/"+city_param+"/district";
    	}else{
    		county_url = jumpUrl+"/area/"+_this+"/district";
    	}
		
		$.get(county_url, function(data){
		  
		  }, "json").error(function(data) {
		    var status_code = $.parseJSON(data.responseText);
		    layer.msg(status_code.message);
		  }).success(function(data) {
		      var obj = data.data;
		      var options = '';
		      $.each(obj, function(key, item) {
		      		options+='<option value="'+item.key+'">'+item.value+'</option>';
		      });
		      $("#district").append(options);
		      if(district){
		      	$("#district option[value='"+district+"']").attr("selected",true);
		      }
		  });
	}

	//上传图片的插件
	//初始化jQuery的文件上传控件
	//上传头像
	$('#avatar_file').fileupload({
			limitMultiFileUploads : 1,
			limitMultiFileUploadSize : 1,
			limitMultiFileUploadSizeOverhead : 512,
			limitConcurrentUploads : 1,
		    sequentialUploads : true,
			headers: 
				{"Accept":"application/json"}, //必须有
	        dataType: "json", //表示返回值类型，不必须
			formData:{
				user_id:userId,
				_token:token
			},
			messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
		    done: function (e, data) {//设置文件上传完毕事件的回调函数
		    	var obj = data.result;
		    	var avatar = obj.data.file_path;
		    	if(data.result=='error1'){
		    		layer.alert('不允许的文件上传类型，仅允许jpg,png,jpeg,gif格式！', 9, !1);
		    		return false;
		    	}else if(data.result=='error2'){
		    		layer.alert('图片尺寸过大,尺寸需小于1M！', 9, !1);
		    		return false;
		    	}
		    	$("input:hidden[name='avatar']").val(avatar);
		    	layer.msg("上传头像成功！");
		    	$("#avatar_peituImg").attr("src", jumpUrl+'/file/show?path='+avatar);//预览图片
		    	$(".avatar_js_cover").show();
		    },
		    fail: function(e, data) {
	                  //错误提示
	                  console.info(data)
	                  console.info(data.result)
		    }
		});
		

		//上传主页背景图片
	$('#bg_image_file').fileupload({
			limitMultiFileUploads : 1,
			limitMultiFileUploadSize : 1,
			limitMultiFileUploadSizeOverhead : 512,
			limitConcurrentUploads : 1,
		    sequentialUploads : true,
			headers: 
				{"Accept":"application/json"}, //必须有
	        dataType: "json", //表示返回值类型，不必须
			formData:{
				user_id:userId,
				_token:token
			},
			messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
		    done: function (e, data) {//设置文件上传完毕事件的回调函数  
		    	var obj = data.result;
		    	var bg_image = obj.data.file_path; 
		    	if(data.result=='error1'){
		    		layer.alert('不允许的文件上传类型，仅允许jpg,png,jpeg,gif格式！', 9, !1);
		    		return false;
		    	}else if(data.result=='error2'){
		    		layer.alert('图片尺寸过大,尺寸需小于1M！', 9, !1);
		    		return false;
		    	}
		    	//预览图片
		    	// $("input:hidden[name='bg_image']").val(bg_image);
		    	// $("#bg_imageImg").attr("src", jumpUrl+'/'+bg_image);
		    	layer.msg("背景图片上传成功");
		    	window.location.reload();
		    	 

		    },
		    fail: function(e, data) {
	                  //错误提示
	                  console.info('1',data)
	                  console.info('2',data.result)
		    }
		});



})


